﻿using System;

using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace FacebookApp
{
    public partial class Admins_Order : System.Web.UI.Page
    {
        //private static bool Insert;

        private string user_id = "-1";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["WB_UserId"] != null)
                    user_id = Session["WB_UserId"].ToString();

                Session["folder"] = "Order";
                BindGrid();
            }
        }

        private void BindGrid()
        {
            grdOrder.DataSource = OrderService.Order_GetByTop("", "", " [Date] DESC");
            grdOrder.DataBind();
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            pnShow.Visible = false;
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            Session["upload"] = null;
           // Insert = true;
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridItem item = default(DataGridItem);
                for (int i = 0; i < grdOrder.Items.Count; i++)
                {
                    item = grdOrder.Items[i];
                    if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                    {
                        if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                        {
                            string strId = item.Cells[1].Text;
                            try
                            {
                                OrderService.Order_Delete(strId);
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                grdOrder.CurrentPageIndex = 0;
                BindGrid();
            }
            catch
            {
            }
        }

        protected void grdOrder_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strCA;
            strCA = e.CommandArgument.ToString();
            txtID.Text = strCA;
            if (e.CommandName == "Edit")
            {
                //thong tin khac hang
                List<Order> lod = new List<Order>();
                Order ord = OrderService.Order_GetById(strCA);
                if (ord.Status == 0)
                    lbtUpdate.Visible = true;
                else
                    lbtUpdate.Visible = false;
                lod.Add(ord);
                grdCustomer.DataSource = lod;
                grdCustomer.DataBind();

                //chi tiet don hang
                List<OrderDetail> lodd = new List<OrderDetail>();
                lodd = OrderDetailService.OrderDetail_GetByTop("", " [Order_Id] = " + strCA, "");
                //lodd.Add(OrderDetailService.OrderDetail_GetByOrderId(strCA));
                grdshopcard.DataSource = lodd;
                grdshopcard.DataBind();

                double tong = 0;
                Label lbltotal;
                for (int i = 0; i < grdshopcard.Items.Count; i++)
                {
                    lbltotal = (Label)grdshopcard.Items[i].FindControl("lbltotal");
                    lbltotal.Text = lbltotal.Text.Substring(0, lbltotal.Text.LastIndexOf("VNĐ"));
                    lbltotal.Text = lbltotal.Text.Replace(".", "");
                    tong += (double.Parse(lbltotal.Text));
                }
                lblsumpay.Text = Format_Price(tong.ToString());

                pnUpdate.Visible = true;
                pnShow.Visible = false;
            }
            else
            {
                OrderService.Order_Delete(strCA);
                BindGrid();
            }

        }        

        protected string Tensanpham(string ID)
        {
            Ebook eb = EbookService.Ebook_GetById(ID);
            return eb.Name;
        }

        protected string Format_Price(string Price)
        {
            return FunctionClass.Format_Price(Price);
        }

        protected void lblBack_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnShow.Visible = true;
        }

        protected void lblUpdate_Click(object sender, EventArgs e)
        {

            OrderService.Order_UpdateStatus(txtID.Text, "1");
            
            Label lblProductID;
            Label lblQuantity;
            for (int i = 0; i < grdshopcard.Items.Count; i++)
            {
                lblProductID = (Label)grdshopcard.Items[i].FindControl("lblProductID");
                lblQuantity = (Label)grdshopcard.Items[i].FindControl("lblQuantity");
                EbookService.Ebook_Update_NumBought(lblProductID.Text, lblQuantity.Text);
            }

            
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            BindGrid();
        }

        protected void grdOrder_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdOrder.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grdOrder_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdOrder.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut",
                                              "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                        ((CheckBox) checkBox).Attributes.Add("onClick",
                                                             "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex +
                                                             ")");
                    }
                }
            }
        }
    }
}
